
/**
 * 求圆周上等分点的坐标
 * @param {*} r 半径
 * @param {*} ox 圆心x坐标
 * @param {*} oy 圆心y坐标
 * @param {*} count 等分个数
 */

const host = location.host;

let devHostReg = /^(http?(s)?:\/\/)?((171\.221\.172\.)|(192\.168\.1\.)|(localhost))(.+)$/;//匹配开发环境的正则表达式
// let prodHostReg = /^(http?(s)?:\/\/)?((i\.))(.+)$/;//匹配正式环境的正则表达式
let testHostReg = /^(http?(s)?:\/\/)?((183\.196\.99\.))(.+)$/;//匹配183环境的正则表达式

let defaultUrl = "";
let wsUrl = "";
let uploadUrl = "";
if (devHostReg.test(host)) {
    defaultUrl = "http://120.79.162.253:8072/";
    uploadUrl = "http://120.79.162.253:7002/";
    wsUrl = "171.221.172.80:8013";
}else if(testHostReg.test(host)){
    defaultUrl = "http://183.196.99.169:7003/";
    uploadUrl = "http://183.196.99.169:7003";
    wsUrl = "183.196.99.169:7003";
} else {
    defaultUrl = "http://10.238.30.195:7003/";
    uploadUrl = "http://10.238.30.195:7003";
    wsUrl = "10.238.30.195:7003";
}

export { defaultUrl, uploadUrl, wsUrl }
export let isDev = true;//是否是开发环境

export function getPoint(r, ox, oy, count) {
    var radians = (Math.PI / 180) * Math.round(360 / count), //弧度
        i = 0;
    var point = [];
    for (; i < count; i++) {

        if (count == 2) {//处理水平排列
            var x = ox + r * Math.cos(radians * i),
                y = oy + r * Math.sin(radians * i);
        } else {
            var x = ox + r * Math.sin(radians * i),
                y = oy + r * Math.cos(radians * i);
        }
        point.unshift({ x: x, y: y }); //为保持数据顺时针
    }
    return point;
}
/**
 * 将一个数组按指定长度分割，组成一个新的数组集合
 * @param {*} array 
 * @param {*} size 
 */
export function chunkArr(array = [], size = 1) {
    //获取数组的长度，如果你传入的不是数组，那么获取到的就是undefined
    const length = array.length
    //判断不是数组，或者size没有设置，size小于1，就返回空数组
    if (!length || !size || size < 1) {
        return []
    }
    //核心部分
    let index = 0 //用来表示切割元素的范围start
    let resIndex = 0 //用来递增表示输出数组的下标

    //根据length和size算出输出数组的长度，并且创建它。
    let result = new Array(Math.ceil(length / size))
    //进行循环
    while (index < length) {
        //循环过程中设置result[0]和result[1]的值。该值根据array.slice切割得到。
        result[resIndex++] = array.slice(index, (index += size))
    }
    //输出新数组
    return result
}
/**
 * 设置cookie
 * @param {*} key cookie的key值
 * @param {*} value cookie的value值
 * @param {*} day cookie的有效天数
 */
export function setCookie(key, value, day = 1) {
    return defaultSetCookie(key, value, day);
}
/**
 * 获取cookie
 * @param {*} key 
 */
export function getCookie(key) {
    let cookieArr = document.cookie.split('; ');
    for (let i = 0; i < cookieArr.length; i++) {
        let arr = cookieArr[i].split('=');
        if (arr[0] === key) {
            return decodeURIComponent(arr[1]);
        }
    }
    return "";
}
/**
 * 删除cookie
 * @param {*} key 
 */
export function removeCookie(key) {
    //这里只需要把Cookie有效期退回一天便可以删除
    return defaultSetCookie(key, "", -1);
}
/**
 * 设置cookie的方法，因为会有多个地方调用，所以提成公用
 */
function defaultSetCookie(key, value, day = 1) {
    if (typeof value === "object") {
        value = JSON.stringify(value);
    }
    let exp = new Date();
    exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);//设置cookie有效期
    document.cookie = key + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=/";

}
/**
 * 转换时间格式
 * @param {*} tamp 时间戳
 * @param {*} fmt 时间格式
 */
export function getTimeStr(tamp, fmt = "yyyy-MM-dd hh:mm:ss") {
    if (!tamp) {
        return "-";
    }
    return new Date(Number(tamp)).Format(fmt);
}

/**
 * 获取截止到目前过去的时间
 * @param {*} tamp 时间戳
 */
export function getPastTime(tamp) {
    if (!tamp) return "-";
    let nowTime = new Date().getTime();
    let min = (nowTime - tamp) / (1000 * 60);
    // let m = parseInt(min % 60);
    return min;
}
/**
 * 校验正则表达式
 */
export const invalid = {
    mobileReg: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
    allPhone : /^[0-9\-]+$/,
}

/**
 * 根据分钟数换算天/时/分
 * @param {*} min 分钟数
 */
export function getDateStr(tamp, type) {
    //type换算成什么，day：天
    let now = new Date().getTime();
    let min = (now - tamp) / (1000 * 60);
    var day = parseInt(min / 60 / 24);
    var hour = parseInt((min / 60) % 24);
    var m = parseInt(min % 60);
    min = "";
    if (day > 0) {
        min = day + "天";
    }
    if (hour > 0) {
        min += hour + "小时";
    }
    if (m > 0) {
        min += parseFloat(m) + "分钟";
    }
    if (type == 'day') {
        return day
    } else {
        return min == "" ? "0分钟" : min;
    }
}
function handleScroll(id, callback) {
    let scrollObj = document.getElementById(id); // 滚动区域
    let clientHeight = scrollObj.clientHeight; // div 的高度
    let scrollTop = scrollObj.scrollTop; // div 到头部的距离
    let scrollHeight = scrollObj.scrollHeight; // 滚动条的总高度

    let sHeight = scrollTop + clientHeight;//滚动条高度+div的高度；
    let height = scrollHeight - 30;//离底部的高度；
    callback && callback(sHeight, height);
}
/**
 * 监听滚动事件
 * @param {*} id 
 * @param {*} callback 
 */
export function addScrollEvent(id, callback) {
    let dom = document.getElementById(id);
    dom.addEventListener('scroll', handleScroll.bind(null, id, callback), true);

}
/**
 * 移除滚动事件
 * @param {}} id 
 */
export function removeScrollEvent(id) {
    let dom = document.getElementById(id);
    dom.removeEventListener('scroll');
}

export function getNowDate() {
    let tamp = new Date();
    let weekList = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
    ];
    let date = tamp.Format("yyyy-MM-dd");
    let hours = tamp.Format("hh");
    let minutes = tamp.Format("mm");
    let seconds = tamp.Format("ss");
    let weekDay = weekList[tamp.getDay()];
    return { date, hours, minutes, seconds, weekDay };
}

export const delay = { //通过requestAnimationFrame重写setTimeout/setInterval，避免性能问题
    intervalTimer: null,
    timeoutTimer: null,
    setTimeout(cb, interval) { // 实现setTimeout功能
        let now = Date.now
        let stime = now()
        let etime = stime
        let loop = () => {
            this.timeoutTimer = requestAnimationFrame(loop)
            etime = now()
            if (etime - stime >= interval) {
                cb()
                cancelAnimationFrame(this.timeoutTimer)
            }
        }
        this.timeoutTimer = requestAnimationFrame(loop)
        return this.timeoutTimer
    },
    clearTimeout() {
        cancelAnimationFrame(this.timeoutTimer)
    },
    setInterval(cb, interval) { // 实现setInterval功能
        let now = Date.now
        let stime = now()
        let etime = stime
        let loop = () => {
            this.intervalTimer = requestAnimationFrame(loop)
            etime = now()
            if (etime - stime >= interval) {
                stime = now()
                etime = stime
                cb()
            }
        }
        this.intervalTimer = requestAnimationFrame(loop)
        return this.intervalTimer
    },
    clearInterval() {
        cancelAnimationFrame(this.intervalTimer)
    }
}
/**
 * 每隔3位数加一个逗号，
 * @param {*} num 
 */
export const toThousands = (num)=> {
    var result = [], counter = 0;
    num = (num || 0).toString().split('');
    for (var i = num.length - 1; i >= 0; i--) {
        counter++;
        result.unshift(num[i]);
        if (!(counter % 3) && i != 0) { result.unshift(','); }
    }
    return result.join('');
}